﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Filter.Special
{
    [Serializable]
    public class TitleHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "标题过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }

        public override string Description
        {
            get
            {
                return "获取网页标题";
            }
        }

        public TitleHTMLAnalyze()
            : base()
        { 
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            OutputKey = "Title";
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "//title";
            RunOnce = true;
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNode targetNode = doc.DocumentNode.SelectSingleNode(FilterExpression);
                if (targetNode != null)
                {
                    HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + targetNode.StreamPosition); // HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition));
                    FilterResult result = new FilterResult();
                    result.HTMLPosition = pr.HTMLPosition + targetNode.StreamPosition; //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition);
                    result.HTMLResult = string.Format("{0}", targetNode.InnerText);
                    listRevalue.Add(result);
                    break;
                }

            }

            //List<string> s = base.FilterHTML();
            //if (s.Count == 0) return null;
            //StringBuilder sb = new StringBuilder();

            //List<string> listRevalue = new List<string>();
            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //foreach (var st in s)
            //{
            //    doc.LoadHtml(st);
            //    if (doc.DocumentNode != null)
            //        sb.AppendFormat("{0}", doc.DocumentNode.InnerText);
            //}

            return listRevalue;
        }
    }

    [Serializable]
    public class KeywordsHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "关键字过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }

        public override string Description
        {
            get
            {
                return "获取网页关键字";
            }
        }


        public KeywordsHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            OutputKey = "Keywords";
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "//meta[@name='keywords']";
            RunOnce = true;
        }

        public override List<FilterResult> FilterHTML()
        {

            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNode targetNode = doc.DocumentNode.SelectSingleNode(FilterExpression);
                if (targetNode != null)
                {
                    foreach (var attr in targetNode.Attributes)
                    {
                        if (attr.Name.ToLower().CompareTo("content") == 0)
                        {
                            HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + targetNode.StreamPosition); //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition));
                            FilterResult result = new FilterResult();
                            result.HTMLPosition = pr.HTMLPosition + targetNode.StreamPosition ; //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition);
                            result.HTMLResult = string.Format("{0}", attr.Value);
                            listRevalue.Add(result);
                        }
                    }
                }
            }
            return listRevalue;
            //string s = base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
            //{
            //    if (attr.Name.ToLower().CompareTo("content") == 0)
            //    {
            //        sb.AppendFormat("{0}", attr.Value);
            //    }
            //}

            //return sb.ToString();
        }
    }

    [Serializable]
    public class DescriptionHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "描述过滤器";
            }
        }
        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }

        public override string Description
        {
            get
            {
                return "获取网页描述";
            }
        }


        public DescriptionHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            OutputKey = "Description";
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "//meta[@name='description'] | //meta[@name='Description']";
            RunOnce = true;
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNode targetNode = doc.DocumentNode.SelectSingleNode(FilterExpression);
                if (targetNode != null)
                {
                    foreach (var attr in targetNode.Attributes)
                    {
                        if (attr.Name.ToLower().CompareTo("content") == 0)
                        {
                            HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + targetNode.StreamPosition); //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition));
                            FilterResult result = new FilterResult();
                            result.HTMLPosition = pr.HTMLPosition + targetNode.StreamPosition ; //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition);
                            result.HTMLResult = string.Format("{0}", attr.Value);
                            listRevalue.Add(result);
                        }
                    }
                }
            }
            return listRevalue;
            //string s= base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
            //{
            //    if (attr.Name.ToLower().CompareTo("content") == 0)
            //    {
            //        sb.AppendFormat("{0}", attr.Value);                
            //    }
            //}

            //return sb.ToString();
        }
    }

    [Serializable]
    public class ImgSrcHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "图片地址过滤器";
            }
        }
        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }
        public override string Description
        {
            get
            {
                return "获取图片Url";
            }
        }


        public ImgSrcHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "//img";
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNode targetNode = doc.DocumentNode.SelectSingleNode(FilterExpression);
                if (targetNode != null)
                {
                    foreach (var attr in targetNode.Attributes)
                    {
                        if (attr.Name.ToLower().CompareTo("src") == 0)
                        {
                            HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + targetNode.StreamPosition); //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition));
                            FilterResult result = new FilterResult();
                            result.HTMLPosition = pr.HTMLPosition + targetNode.StreamPosition ; //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition);

                            NetGatherUtility.HyperLink hlnk = new CaiyinSoft.NetGather.NetGatherUtility.HyperLink("", attr.Value, this.HTMLAnalyzer.GetInputHTMLUrl());

                            result.HTMLResult = string.Format("{0}", hlnk.FullHref);
                            listRevalue.Add(result);
                            break;
                        }
                    }
                }
            }
            return listRevalue;

            //string s = base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
            //{
            //    if (attr.Name.ToLower().CompareTo("src") == 0)
            //    {
            //        sb.AppendFormat("{0}", attr.Value);
            //    }
            //}

            //return sb.ToString();
        }
    }

    [Serializable]
    public class LinkHrefHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "超链接地址过滤器";
            }
        }
        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }
        public override string Description
        {
            get
            {
                return "获取超链接指向Url";
            }
        }


        public LinkHrefHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "//a";
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNode targetNode = doc.DocumentNode.SelectSingleNode(FilterExpression);
                if (targetNode != null)
                {
                    foreach (var attr in targetNode.Attributes)
                    {
                        if (attr.Name.ToLower().CompareTo("href") == 0)
                        {
                            HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + targetNode.StreamPosition); //HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition));
                            FilterResult result = new FilterResult();
                            result.HTMLPosition = pr.HTMLPosition +targetNode.StreamPosition ; // HTMLAnalyzer.GetPosition(targetNode.Line, targetNode.LinePosition);

                            NetGatherUtility.HyperLink hlnk = new CaiyinSoft.NetGather.NetGatherUtility.HyperLink("", attr.Value, this.HTMLAnalyzer.GetInputHTMLUrl());

                            result.HTMLResult = string.Format("{0}", hlnk.FullHref);
                            listRevalue.Add(result);
                            break;
                        }
                    }
                }


                //foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
                //{
                //    if (attr.Name.ToLower().CompareTo("href") == 0)
                //    {
                //        HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition));
                //        FilterResult result = new FilterResult();
                //        result.HTMLPosition = pr.HTMLPosition + HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition);
                //        result.HTMLResult = string.Format("{0}", attr.Value);
                //        listRevalue.Add(result);
                //    }
                //}
            }
            return listRevalue;

            //string s = base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
            //{
            //    if (attr.Name.ToLower().CompareTo("href") == 0)
            //    {
            //        sb.AppendFormat("{0}", attr.Value);
            //    }
            //}

            //return sb.ToString();
        }
    }

    [Serializable]
    public class InnerTextHTMLAnalyze : XPathHTMLAnalyze
    {

        public override string FilterCaption
        {
            get
            {
                return "内文本过滤器";
            }
        }
        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }
        public override string Description
        {
            get
            {
                return "获取元素InnerText";
            }
        }

        public InnerTextHTMLAnalyze():base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "*";
        }

        public override List<FilterResult> FilterHTML()
        {

            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNodeCollection contents = doc.DocumentNode.SelectNodes(FilterExpression);
                if (contents != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode content in contents)
                    {
                        HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + doc.DocumentNode .StreamPosition); //HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition));
                        FilterResult result = new FilterResult();
                        result.HTMLPosition = pr.HTMLPosition + doc.DocumentNode.StreamPosition; // HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition);
                        result.HTMLResult = string.Format("{0}",content.InnerText );
                        listRevalue.Add(result);
                    }
                }
            }
            return listRevalue;

            //string s = base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //HtmlAgilityPack.HtmlNodeCollection contents = doc.DocumentNode.SelectNodes(FilterExpression);
            //if (contents != null)
            //{
            //    foreach (HtmlAgilityPack.HtmlNode content in contents)
            //    {
            //        sb.AppendFormat("{0}", content.InnerText);
            //    }
            //}

            //return sb.ToString();
        }
    }

    [Serializable]
    public class AttributeHTMLAnalyze : XPathHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "属性过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "元素属性";
            }
        }

        public override string Description
        {
            get
            {
                return "获取指定属性值";
            }
        }

        public AttributeHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNodeCollection contents = doc.DocumentNode.SelectNodes("*");
                if (contents != null)
                {
                    foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
                    {
                        if (attr.Name.ToLower().CompareTo(FilterExpression.ToLower()) == 0)
                        {
                            HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition  + doc.DocumentNode.StreamPosition); // + HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition));
                            FilterResult result = new FilterResult();
                            result.HTMLPosition = pr.HTMLPosition + doc.DocumentNode.StreamPosition; // HTMLAnalyzer.GetPosition(doc.DocumentNode.Line, doc.DocumentNode.LinePosition);
                            result.HTMLResult = string.Format("{0}", attr.Value);
                            listRevalue.Add(result);
                        }
                    }
                }
            }
            return listRevalue;

            //string s = GetInputHTML();// base.FilterHTML();
            //if (s.Trim().Length == 0) return "";
            //StringBuilder sb = new StringBuilder();

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(s);
            //HtmlAgilityPack.HtmlNodeCollection contents = doc.DocumentNode.SelectNodes("*");
            //if (contents != null)
            //{
            //    foreach (var attr in doc.DocumentNode.ChildNodes[0].Attributes)
            //    {
            //        if (attr.Name.ToLower().CompareTo(FilterExpression.ToLower()) == 0)
            //        {
            //            sb.AppendFormat("{0}", attr.Value);
            //        }
            //    }
            //}

            //return sb.ToString();
        }
    }


}
